package org.dolphin.entity.inventory;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import org.dolphin.base.constant.DatabaseAttribute;
import org.dolphin.entity.interceptor.Auditable;
import org.dolphin.entity.security.User;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.springframework.util.ObjectUtils;

@Entity
@Table(name = "MS_PRODUCT", uniqueConstraints = {@UniqueConstraint(columnNames = {"NAME"}, name = "UQ_MS_PRODUCT_NAME")})
public class Product implements Serializable, Auditable {
  private static final long serialVersionUID = -3088823190479466737L;
  private String id;
  private String name;
  private List<ProductCategoryLink> productCategoryLinks = new ArrayList<ProductCategoryLink>();
  private List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
  private Long version;
  private User createdBy;
  private User updatedBy;
  private Date createdDate;
  private Date updatedDate;
  private BigDecimal baseRetailPrice;

  @Transient
  public void addProductAttribute(ProductAttribute productAttribute) {
    if (productAttribute != null) {
      productAttribute.setProduct(this);
      productAttributes.add(productAttribute);
    }
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof Product) {
      Product oth = (Product) obj;
      return ObjectUtils.nullSafeEquals(getId(), oth.getId());
    }
    return false;
  }

  @Column(name = "BASE_RETAIL_PRICE", nullable = false, precision = DatabaseAttribute.MONEY_PRECISION, scale = DatabaseAttribute.MONEY_SCALE)
  public BigDecimal getBaseRetailPrice() {
    return baseRetailPrice;
  }

  @Override
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "CREATED_BY", nullable = false)
  @ForeignKey(name = "FK_MS_PRODUCT_CREATED_BY")
  public User getCreatedBy() {
    return createdBy;
  }

  @Override
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "CREATED_DATE", nullable = false)
  public Date getCreatedDate() {
    return createdDate;
  }

  @Id
  @GeneratedValue(generator = "uuid2")
  @GenericGenerator(name = "uuid2", strategy = "org.dolphin.base.id.generator.CustomUUIDGenerator")
  @Column(name = "ID", nullable = false, length = DatabaseAttribute.ID_LENGTH)
  public String getId() {
    return id;
  }

  @Column(name = "NAME", length = 255, nullable = false)
  public String getName() {
    return name;
  }

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "product", cascade = {CascadeType.ALL})
  @LazyCollection(LazyCollectionOption.EXTRA)
  public List<ProductAttribute> getProductAttributes() {
    return productAttributes;
  }

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.product", cascade = {CascadeType.ALL}, orphanRemoval = true)
  @LazyCollection(LazyCollectionOption.EXTRA)
  public List<ProductCategoryLink> getProductCategoryLinks() {
    return productCategoryLinks;
  }

  @Override
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "UPDATED_BY", nullable = true)
  @ForeignKey(name = "FK_MS_PRODUCT_UPDATED_BY")
  public User getUpdatedBy() {
    return updatedBy;
  }

  @Override
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "UPDATED_DATE", nullable = true)
  public Date getUpdatedDate() {
    return updatedDate;
  }

  @Version
  @Column(name = "VERSION_NO", nullable = false)
  public Long getVersion() {
    return version;
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getId());
    return hash;
  }

  public void setBaseRetailPrice(BigDecimal baseRetailPrice) {
    this.baseRetailPrice = baseRetailPrice;
  }

  @Override
  public void setCreatedBy(User createdBy) {
    this.createdBy = createdBy;
  }

  @Override
  public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
  }

  public void setId(String id) {
    this.id = id;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setProductAttributes(List<ProductAttribute> productAttributes) {
    this.productAttributes = productAttributes;
  }

  public void setProductCategoryLinks(List<ProductCategoryLink> productCategoryLinks) {
    this.productCategoryLinks = productCategoryLinks;
  }

  @Override
  public void setUpdatedBy(User updatedBy) {
    this.updatedBy = updatedBy;
  }

  @Override
  public void setUpdatedDate(Date updatedDate) {
    this.updatedDate = updatedDate;
  }

  public void setVersion(Long version) {
    this.version = version;
  }
}
